Public Class admin
    Inherits System.Web.UI.Page
    Protected WithEvents lbl_stat As System.Web.UI.WebControls.Label
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents Lbl_date As System.Web.UI.WebControls.Label
    Protected WithEvents Label6 As System.Web.UI.WebControls.Label
    Protected WithEvents lbl_user As System.Web.UI.WebControls.Label
    Protected WithEvents Label3 As System.Web.UI.WebControls.Label
    Protected WithEvents dg_apps As System.Web.UI.WebControls.DataGrid
    Protected WithEvents txt_nt As System.Web.UI.WebControls.TextBox
    Protected WithEvents txt_name As System.Web.UI.WebControls.TextBox
    Protected WithEvents Label2 As System.Web.UI.WebControls.Label
    Protected WithEvents btn_create As System.Web.UI.WebControls.Button
    Protected WithEvents btn_update As System.Web.UI.WebControls.Button
    Protected WithEvents btn_del As System.Web.UI.WebControls.Button
    Protected WithEvents ddl_users As System.Web.UI.WebControls.DropDownList
    Protected WithEvents HyperLink1 As System.Web.UI.WebControls.HyperLink
    Protected WithEvents Image1 As System.Web.UI.WebControls.Image
    Protected WithEvents Image2 As System.Web.UI.WebControls.Image
    Protected WithEvents HyperLink2 As System.Web.UI.WebControls.HyperLink

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Public Sub fill_users()
        Dim sql As String
        Dim ds As New myDBConn
        Dim i As Integer
        Dim myitem As DataGridItem
        Me.ddl_users.Items.Clear()
        sql = "select * from users order by logusr"
        Me.ddl_users.Items.Add("**New user**")


        For i = 0 To ds.myOleDbTable(sql).Rows.Count - 1
            Me.ddl_users.Items.Add(ds.myOleDbTable(sql).Rows(i)("logusr"))
            Me.ddl_users.Items(i + 1).Value = ds.myOleDbTable(sql).Rows(i)("usrid")
        Next


        'Me.ddl_users.DataSource = ds.myOleDbTable(sql)
        'Me.ddl_users.DataTextField = "logusr"
        'Me.ddl_users.DataValueField = "usrid"
        Me.ddl_users.DataBind()

        For Each myitem In Me.dg_apps.Items
            If CType(myitem.FindControl("chk"), CheckBox).Checked = True Then
                CType(myitem.FindControl("chk"), CheckBox).Checked = False
            End If
        Next
        If Me.ddl_users.SelectedItem.Text = "**New user**" Then
            Me.btn_del.Enabled = False
            Me.btn_update.Enabled = False
            Me.btn_create.Enabled = True
            Me.txt_name.Text = ""
            Me.txt_nt.Text = ""
        Else
            Me.btn_del.Enabled = True
            Me.btn_update.Enabled = True
            Me.btn_create.Enabled = False

        End If

        ds = Nothing
    End Sub

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here

        Dim SQL As String
        Dim sql1 As String
        Dim usr As String
        Dim ds As New myDBConn
        Session.LCID = 1055

        If Page.IsPostBack Then Exit Sub
        Me.Lbl_date.Text = Now.ToShortDateString
        usr = Request.ServerVariables("LOGON_USER")
        SQL = "SELECT a.name FROM USERS a,perms p WHERE  a.usrid=p.usrid and a.LOGUSR= '" & usr & "' and appid=2;"

        If ds.myOleDbTable(SQL).Rows.Count > 0 Then
            Me.lbl_user.Text = ds.myOleDbTable(SQL).Rows(0)(0)
        Else
            Response.Redirect("logout.htm")
        End If
        sql1 = "select * from apps"
        Me.dg_apps.DataSource = ds.myOleDbTable(sql1)
        Me.dg_apps.DataBind()

        ds = Nothing
        fill_users()
    End Sub


    Private Sub btn_create_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_create.Click
        Dim str As String
        Dim sqlstr As String
        Dim sql As String
        Dim tmp As Integer
        Dim ds As New myDBConn
        Dim userid As Integer
        str = "select * from users where logusr =trim('" & Me.txt_nt.Text & "') or name like '%" & Me.txt_name.Text & "%';"

        If ds.myOleDbTable(str).Rows.Count > 0 Then
            Response.Write("<SCRIPT language=""JavaScript"">" & vbCrLf & "{" & vbCrLf)
            Response.Write("alert(""This user already exists in the database!!"")" & vbCrLf)
            Response.Write("}" & vbCrLf & "</SCRIPT>" & vbCrLf)
            Exit Sub
        End If

        str = "insert into Users (logusr,name) values ('" & Me.txt_nt.Text & "','" & Me.txt_name.Text & "');"
        ds.Execute(str)
        sqlstr = "select max(usrid) from users;"
        userid = ds.myOleDbTable(sqlstr).Rows(0)(0)

        Dim myitem As DataGridItem
        For Each myitem In Me.dg_apps.Items
            If CType(myitem.FindControl("chk"), CheckBox).Checked Then
                tmp = CType(dg_apps.DataKeys(myitem.ItemIndex), Integer)
                sql = "insert into perms (appid,usrid) values (" & tmp & "," & userid & ");"
                ds.Execute(sql)
            End If
        Next
        ds = Nothing
        fill_users()
    End Sub

    Private Sub btn_del_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_del.Click
        Dim sql As String
        Dim ds As New myDBConn
        If Me.ddl_users.SelectedItem.Text <> "**New user**" Then
            sql = "delete from users where usrid=" & Me.ddl_users.SelectedItem.Value & ";"
            ds.Execute(sql)
            sql = "delete from perms where usrid=" & Me.ddl_users.SelectedItem.Value & ";"
            ds.Execute(sql)
            fill_users()
        Else
            Response.Write("<SCRIPT language=""JavaScript"">" & vbCrLf & "{" & vbCrLf)
            Response.Write("alert(""Please choose a user!!"")" & vbCrLf)
            Response.Write("}" & vbCrLf & "</SCRIPT>" & vbCrLf)
        End If
        ds = Nothing
        fill_users()
    End Sub

    Private Sub ddl_users_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddl_users.SelectedIndexChanged
        Dim sql As String
        Dim ds As New myDBConn
        Dim tmp As Integer
        Dim myitem As DataGridItem
        If Me.ddl_users.SelectedItem.Text <> "**New user**" Then
            Me.btn_del.Enabled = True
            Me.btn_update.Enabled = True
            Me.btn_create.Enabled = False
            sql = "select a.logusr,a.name,a.usrid,b.appid from perms b,users a where a.usrid=b.usrid and  a.usrid=" & Me.ddl_users.SelectedItem.Value & ";"
            Me.txt_name.Text = ds.myOleDbTable(sql).Rows(0)(1)
            Me.txt_nt.Text = ds.myOleDbTable(sql).Rows(0)(0)

            For Each myitem In Me.dg_apps.Items
                tmp = CType(dg_apps.DataKeys(myitem.ItemIndex), Integer)
                sql = "select * from perms where appid=" & tmp & " and usrid= " & Me.ddl_users.SelectedItem.Value & ";"
                If ds.myOleDbTable(sql).Rows.Count > 0 Then
                    CType(myitem.FindControl("chk"), CheckBox).Checked = True
                Else
                    CType(myitem.FindControl("chk"), CheckBox).Checked = False
                End If
            Next
        Else

            For Each myitem In Me.dg_apps.Items
                If CType(myitem.FindControl("chk"), CheckBox).Checked = True Then
                    CType(myitem.FindControl("chk"), CheckBox).Checked = False
                End If
            Next
            Me.btn_del.Enabled = False
            Me.btn_update.Enabled = False
            Me.btn_create.Enabled = True
            Me.txt_name.Text = ""
            Me.txt_nt.Text = ""
        End If

    End Sub

    Private Sub btn_update_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_update.Click
        Dim sql As String
        Dim tmp As Integer
        Dim sql1 As String
        Dim sqlstr As String
        Dim ds As New myDBConn
        sql = "update users set logusr='" & Me.txt_nt.Text & "',name='" & Me.txt_name.Text & "' where usrid=" & Me.ddl_users.SelectedItem.Value & ";"
        sql1 = "delete from perms where usrid=" & Me.ddl_users.SelectedItem.Value & ";"
        ds.Execute(sql)
        ds.Execute(sql1)
        Dim myitem As DataGridItem
        For Each myitem In Me.dg_apps.Items
            If CType(myitem.FindControl("chk"), CheckBox).Checked Then
                tmp = CType(dg_apps.DataKeys(myitem.ItemIndex), Integer)
                sqlstr = "insert into perms (appid,usrid) values (" & tmp & "," & Me.ddl_users.SelectedItem.Value & ");"
                ds.Execute(sqlstr)
            End If
        Next
        ds = Nothing
        fill_users()


    End Sub
End Class
